/*
 * @Description: I18n 兼容工具
 * @Author: Cxy
 * @Date: 2023-07-12 16:19:14
 * @LastEditors: Cxy
 * @LastEditTime: 2024-03-01 15:09:51
 * @FilePath: \blogAdmin\play\src\hooks\I18n.ts
 */
import { useI18n } from 'vue-i18n'
import type { FormInstance } from 'element-plus'
import type { Component } from 'vue'
import { langData } from '@/i18n'

type ComFormInstance = Component & { value: FormInstance }

export default function (...com: Component[]) {
  const { t, locale } = useI18n()

  watch(locale, () => com.length && com.forEach(c => (c as ComFormInstance).value?.validate()))

  const tt = computed(() => (val = '') => {
    if (!val) return ''
    if (langData.includes(val)) return t(val)
    const langReg = val.match(/^[a-z]+(?=\.)/gi)
    if (langReg && langData.includes(langReg[0])) return t(val)
    return val
  })

  return {
    tt
  }
}
